﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineStore.DAL;
using WineStore.Models;
using System.Data;
using PagedList;
using PagedList.Mvc;

namespace WineStore.Controllers
{

    public class CategoryController : Controller
    {
        // GET: /Category/ShowCategory/
        public ViewResult ShowCategory(int? page)
        {

            using (var db = new WineDbContext())
            {
                var categories = db.Categories
                    .OrderByDescending(c => c.CategoryId);

                int pageSize = 10;
                int pageNumber = (page ?? 1);

                // Thiet lap thuoc tinh khi phan trang
                PagedListRenderOptions pro = new PagedListRenderOptions();
                pro.MaximumPageNumbersToDisplay = 5;
                pro.LinkToLastPageFormat = "Last";
                pro.DisplayLinkToLastPage = true;
                pro.LinkToFirstPageFormat = "First";
                pro.DisplayLinkToFirstPage = true;
                pro.LinkToNextPageFormat = ">>";
                pro.DisplayLinkToNextPage = true;
                pro.LinkToPreviousPageFormat = "<<";
                pro.DisplayLinkToPreviousPage = true;

                ViewBag.Pro = pro;

                return View(categories.ToPagedList(pageNumber, pageSize));
            }
        }

        // Create Category 
        [HttpGet]
        public ActionResult CreateCategory()
        {
            return View();
        }

        // Create Category 
        [HttpPost]
        public ActionResult CreateCategory(Category category)
        {
            try
            {
                // Validate in server
                if (ModelState.IsValid)
                {
                    using (var db = new WineDbContext())
                    {
                        db.Entry(category).State = EntityState.Added;
                        db.SaveChanges();
                        return RedirectToAction("ShowCategory", "Category");
                    }
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(category);
        }

        // Show Detail Category 
        [HttpGet]
        public ActionResult EditCategory(int id = 0)
        {
            Category category = null;
            using (var db = new WineDbContext())
            {
                category = db.Categories.Find(id);
                if (category == null)
                {
                    return HttpNotFound();
                }
            }
            return View(category);
        }

        /* Khi phương thức SaveChanges được gọi, cờ Modified làm cho Entity Framework cập nhật dòng tương ứng
           trong CSDL. Tất cả các cột sẽ được cập nhật, bao gồm cả các cột người dùng không thay đổi.*/
        // Edit Category 
        [HttpPost]
        public ActionResult EditCategory(Category category)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (var db = new WineDbContext())
                    {
                        db.Entry(category).State = EntityState.Modified;
                        db.SaveChanges();
                        return RedirectToAction("ShowCategory", "Category");
                    }
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(category);
        }

        // GET: /Category/DeleteCategory/
        public ActionResult DeleteCategory(int id)
        {
            Category category = null;
            using (var db = new WineDbContext())
            {
                category = db.Categories.Find(id);
                if (category == null)
                {
                    return HttpNotFound();
                }
            }
            return View(category);
        }

        // POST: /Category/DeleteCategory/
        [HttpPost, ActionName("DeleteCategory")]
        public ActionResult DeleteConfirmed(int id)
        {
            Category category = null;
            try
            {
                using (var db = new WineDbContext())
                {
                    category = db.Categories.Find(id);
                    db.Entry(category).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("ShowCategory", "Category");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(category);
        }

    }
}
